﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;

namespace System
{
    public static class ExtraPropertyDictionaryExssion
    {
        /// <summary>
        ///把ExtraProperty的简单类型数值赋值到o对象里。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="extra"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        public static T? ExtraPropertyDataToObject<T>(this ExtraPropertyDictionary extra, object o)
        {
            
            foreach (var item in extra)
            {
                var p = typeof(T).GetProperty(item.Key);
                var val = extra[item.Key];
                if (p != null && p.CanWrite)
                {
                    if (p.Name == "OrderNo")
                    {
                        if (p.PropertyType == typeof(Nullable<Int32>))
                        { 
                        
                        }
                    }
                    try
                    {
                        if (p.PropertyType.IsEnum)
                        {

                            if (!string.IsNullOrEmpty(val + ""))
                                p.SetValue(o, Enum.Parse(p.PropertyType, val + ""), null);
                            else
                                p.SetValue(o, null, null);
                        }

                        else if (p.PropertyType == typeof(long))
                        {
                            long.TryParse(val + "", out long v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(int))
                        {
                            int.TryParse(val + "", out int v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(double))
                        {
                            double.TryParse(val + "", out double v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(float))
                        {
                            float.TryParse(val + "", out float v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(Guid))
                        {
                            Guid.TryParse(val + "", out Guid v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(DateTime))
                        {
                            DateTime.TryParse(val + "", out DateTime v);
                            p.SetValue(o, v, null);
                        }


                        else if (p.PropertyType == typeof(Nullable<Int64>))
                        {
                            long.TryParse(val + "", out long v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(Nullable<Int32>))
                        {
                            int.TryParse(val + "", out int v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(Nullable<Double>))
                        {
                            double.TryParse(val + "", out double v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(Nullable<Decimal>))
                        {
                            Decimal.TryParse(val + "", out Decimal v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(Nullable<Guid>))
                        {
                            Guid.TryParse(val + "", out Guid v);
                            p.SetValue(o, v, null);
                        }
                        else if (p.PropertyType == typeof(Nullable<DateTime>))
                        {
                            DateTime.TryParse(val + "", out DateTime v);
                            p.SetValue(o, v, null);
                        }



                        else
                        {
                            
                            p.SetValue(o, val, null);
                        }
                    }
                    catch (Exception ex)
                    {
                       // p.SetValue(o, null, null);
                    }




                }
            }
            return default(T);
        }



            
    }
}
